import os

def walk(dir, ext=None, isRecursive=True):
    if not isRecursive:
        for name in os.listdir(dir):
            if ext is not None:
                _, cur_ext = os.path.splitext(name)
                if ext.lower() != cur_ext:
                    continue

            yield os.path.join(dir, name)
    else:
        for root, dirs, files in os.walk(dir):
            for name in files:
                if ext is not None:
                    _, cur_ext = os.path.splitext(name)
                    if ext.lower() != cur_ext:
                        continue

                yield os.path.join(root, name)

def getName(path):
    _, name = os.path.split(path)
    name, _ = os.path.splitext(name)
    return name

class ModTimeCache:
    def __init__(self, path, ext=None, isRecursive=True):
        self.path       = path
        self.ext        = ext
        self.isRecursive = isRecursive
        self.cache      = {}

    def walk(self):
        for path in walk(self.path, self.ext, self.isRecursive):
            curTime = os.path.getmtime(path)
            if self.cache.get(path) == curTime:
                continue

            yield path, curTime

    def __iter__(self):
        for path, time in self.walk():
            self.cache[path] = time
            yield path

    def isModified(self):
        for _ in self.walk():
            return True

    def saveCurrentTime(self):
        self.cache = {}
        for path, time in self.walk():
            self.cache[path] = time

def import_from(path):
    m = __import__(path, globals(), locals(), [], -1)
    for p in path.split('.')[1:]:
        m = getattr(m, p)
    return m